package ${packageBase}.${tableBean.tableNameNoDash}.service;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${packageBase}.${tableBean.tableNameNoDash}.entity.${tableBean.tableNameCapitalized}Entity;
import com.ristone.platform.jdbctemplatetool.JdbcTemplateTool;
import com.ristone.platform.comm.DataTablesPagination;
import com.ristone.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import com.ristone.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;

/**
*${tableBean.tableNameCapitalized} service
*/
@Transactional(readOnly=true)
@Service("${tableBean.tableNameNoDash}Service")
public class ${tableBean.tableNameCapitalized}Service {
   	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private JdbcTemplateTool jdbcTemplateTool;

  
  	@Transactional(readOnly=false)
    public void save(${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}) throws Exception{
    	jdbcTemplateTool.save(${tableBean.tableNameNoDash});
    }
	@Transactional(readOnly=false)
    public int saveAndGetId(${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}) throws Exception{
    	return jdbcTemplateTool.save(${tableBean.tableNameNoDash},"${tableBean.priKey}");
    }
	@Transactional(readOnly=false)
    public void update(${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}) throws Exception{
    	jdbcTemplateTool.update(${tableBean.tableNameNoDash});
    }
	@Transactional(readOnly=false)
    public void saveOrUpdate(${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}) throws Exception {
        if (null == ${tableBean.tableNameNoDash}.get${tableBean.priKey?cap_first}() || 0 == ${tableBean.tableNameNoDash}.get${tableBean.priKey?cap_first}()) {
            save(${tableBean.tableNameNoDash});
        } else {
            update(${tableBean.tableNameNoDash});
        }
    }

    public ${tableBean.tableNameCapitalized}Entity get(Integer id) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
    	return jdbcTemplateTool.get(${tableBean.tableNameCapitalized}Entity.class, id);
    }
 	/**
     * 所有列表
     */
    public List<${tableBean.tableNameCapitalized}Entity> getAll() {
    	String sql="SELECT * FROM ${tableBean.tableName} ";
    	return jdbcTemplateTool.list(sql, null, ${tableBean.tableNameCapitalized}Entity.class);
    }
    /**
     * 分页列表
     * @param page
     * @return
     */
    public DataTablesPagination getListOfPage(DataTablesPagination page,${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}){
    	StringBuffer sql = new StringBuffer(" FROM ${tableBean.tableName} WHERE 1=1");
    	List<Object> params = new ArrayList<Object>();
    	
    	int count =jdbcTemplateTool.count("SELECT count(0)"+sql, params.toArray());
    	page.setiTotalRecords(count);
    	
    	sql.append(" LIMIT ?,?");
    	params.add(page.getiDisplayStart());
    	params.add(page.getiDisplayLength());
    	List<${tableBean.tableNameCapitalized}Entity> list = jdbcTemplateTool.list("SELECT * "+sql, params.toArray(), ${tableBean.tableNameCapitalized}Entity.class);
    	page.setData(list);
    	return page;
    }
     /**
     * 删除
     * @param dream
     * @throws Exception
     */
	@Transactional(readOnly=false)
    public void delete(${tableBean.tableNameCapitalized}Entity ${tableBean.tableNameNoDash}) throws Exception {
   	 	jdbcTemplateTool.delete( ${tableBean.tableNameNoDash});
    }
}
